// tabledeleterow.js version 1.2 2006-02-21
// mredkj.com

// CONFIG notes. Below are some comments that point to where this script can be customized.
// Note: Make sure to include a <tbody></tbody> in your table's HTML

var INPUT_NAME_PREFIX = 'inputName'; // this is being set via script
var RADIO_NAME = 'totallyrad'; // this is being set via script
var TABLE = 'tblLinhHoat'; // this should be named in the HTML
var ROW_BASE = 2; // first number (for display)
var hasLoaded = false;

window.onload=fillInRows2;

function addRows(numrows)
{
	var i;
	hasLoaded = true;
	//alert ('Vao ham');
	if (document.getElementById && document.getElementsByTagName) {
		//alert ('Vao if');
		var tbl = document.getElementById(TABLE);
		var l = tbl.rows.length;
		//alert (l);
		if(l > 1){
			delrow(tbl, l);
		}
		if(numrows){
			/*for(i=0;i<numrows;i++)
				addRowToTable(null);
			}*/
			//alert (numrows);
			i = 0;
			while(i<numrows && i<9){
				addRowTable(null,'','');
				//alert (i);
				i++;
				//alert (numrows);
			}
			if(i>=9){
				document.forms['sanpham'].ADD_SPLTCNT.value = '9';
			}	
		}
	}
	//addRowToTable();
}
function fillInRows2(){
	hasLoaded = true;
}
// CONFIG:
// myRowObject is an object for storing information about the table rows
function myRowObject(one, two, three, four, five, six, seven, eight)
{
	this.one = one; // text object
	this.two = two; // input text object
	this.three = three; // input checkbox object
	this.four = four; // input radio object
	this.five = five;
	this.six = six;
	this.seven = seven;
	this.eight = eight;
}

/*
 * addRowToTable
 * Inserts at row 'num', or appends to the end if no arguments are passed in. Don't pass in empty strings.
 */
function addRowTable(num,BALRATENEW,INTRATEANEW)
{
	//alert(hasLoaded);
	if (hasLoaded && document.getElementById && document.getElementsByTagName) {

	 	var tbl = document.getElementById(TABLE);
		var cboADD_SPLTACR = document.getElementById('ADD_SPLTACR');
		if(cboADD_SPLTACR){
			cboADD_SPLTACR.options[1].selected = true;
		}
		var textbox = document.getElementById('ADD_SPLTCNT');
		//alert(tbl.tBodies[0].rows.length+1);
		var btn_addrecord = document.getElementById('addrecord');
		if(textbox.value>9){
			btn_addrecord.disabled = true;
			return;	
		/*}else{
			btn_addrecord.disabled = false;
			return;*/
		}
		var nextRow = tbl.tBodies[0].rows.length;
		
		var iteration = nextRow + ROW_BASE;
		if (num == null) { 
			num = nextRow;
		} else {
			iteration = num + ROW_BASE;
		}
		// add the row
		
		var row = tbl.tBodies[0].insertRow(num);
		
		// CONFIG: requires classes named classy0 and classy1
		//row.className = 'classy' + (iteration % 2);
	
		// CONFIG: This whole section can be configured
		
		// cell 0 - text
		var cell0 = row.insertCell(0);
		cell0.style.padding = '1px';
		cell0.setAttribute('align','center');
		var textNode = document.createTextNode(iteration);
		cell0.appendChild(textNode);
		
		// cell 1 - input text
		var cell1 = row.insertCell(1);
		cell1.style.padding = '1px';
		//cell1.setAttribute('align','center');
		var txtInp1 = document.createElement('input');
		txtInp1.setAttribute('type', 'text');
		txtInp1.setAttribute('name', 'BALRATENEW' + iteration);
		txtInp1.setAttribute('size', '20');
		txtInp1.onblur = function () {formatNumber(this,this.value)};
		txtInp1.className = 'textright';
		txtInp1.setAttribute('value', BALRATENEW); // iteration included for debug purposes
		cell1.appendChild(txtInp1);
		
		var cell2 = row.insertCell(2);
		//cell2.setAttribute('align','center');
		cell2.style.padding = '1px';
		var txtInp2 = document.createElement('input');
		txtInp2.setAttribute('type', 'text');
		txtInp2.setAttribute('name', 'INTRATEANEW' + iteration);
		txtInp2.setAttribute('size', '20');
		txtInp2.setAttribute('value', INTRATEANEW); // iteration included for debug purposes
		txtInp2.onblur = function () {checkDecimal(this,this.value)};
		txtInp2.className = 'textright';
		cell2.appendChild(txtInp2);
		
		// cell 2 - input button
		var cell3 = row.insertCell(3);
		cell3.style.padding = '1px';
		var btnE2 = document.createElement('input');
		btnE2.setAttribute('type', 'button');
		btnE2.setAttribute('value', '     ');
		btnE2.setAttribute('id','replacement_delete');
		btnE2.onclick = function () {deleteCurrentRow(this)};
		cell3.appendChild(btnE2);
		
		// Pass in the elements you want to reference later
		// Store the myRow object in each row
		row.myRow = new myRowObject(textNode, txtInp1, txtInp2, btnE2);
		//var textbox = document.getElementById('SPLTCNT');
		textbox.value = tbl.tBodies[0].rows.length+1;
	}
}


function deleteCurrentRow(obj)
{
//	alert('vao ham');
	if (hasLoaded) {
		var delRow = obj.parentNode.parentNode;
		var tbl = delRow.parentNode.parentNode;
		var rIndex = delRow.sectionRowIndex;
		var rowArray = new Array(delRow);
		deleteRows(rowArray);
		reorderRows(tbl, rIndex);
		var btn_addrecord = document.getElementById('addrecord');
		if(btn_addrecord){
			btn_addrecord.disabled = false;	
		}
	}

}
function reorderRows(tbl, startingIndex)
{
	if (hasLoaded) {
		//document.maphat.TERMNO.value =  tbl.tBodies[0].rows.length;
		/*var textbox = document.maphat.TERMNO;
		if(textbox){
			alert('Co');	
		}else{
			alert('Khong');
		}*/
		var textbox = document.getElementById('ADD_SPLTCNT');
		textbox.value = tbl.tBodies[0].rows.length+1;
		//alert(tbl.tBodies[0].rows.length);
		if (tbl.rows[startingIndex]) {
			var count = startingIndex + ROW_BASE;
			for (var i=startingIndex; i<tbl.tBodies[0].rows.length; i++) {
			
				// CONFIG: next line is affected by myRowObject settings
				tbl.tBodies[0].rows[i].myRow.one.data = count; // text
				
				// CONFIG: next line is affected by myRowObject settings
				tbl.tBodies[0].rows[i].myRow.two.name = 'BALRATENEW' + count; // input text
				tbl.tBodies[0].rows[i].myRow.three.name = 'INTRATEANEW' + count; // input text
				
				//tbl.tBodies[0].rows[i].myRow.two.value = 'TERM' + count; // input text
				// CONFIG: next line is affected by myRowObject settings
				//var tempVal = tbl.tBodies[0].rows[i].myRow.two.value.split(' '); // for debug purposes
				//tbl.tBodies[0].rows[i].myRow.two.value = count + ' was' + tempVal[0]; // for debug purposes
				
				// CONFIG: next line is affected by myRowObject settings
				//tbl.tBodies[0].rows[i].myRow.four.value = count; // input radio
				
				// CONFIG: requires class named classy0 and classy1
				//tbl.tBodies[0].rows[i].className = 'classy' + (count % 2);
				
				count++;
			}
		}
	}

}

function deleteRows(rowObjArray)
{
	if (hasLoaded) {
		for (var i=0; i<rowObjArray.length; i++) {
			var rIndex = rowObjArray[i].sectionRowIndex;
			rowObjArray[i].parentNode.deleteRow(rIndex);
		}
	}
	
}

function openInNewWindow(frm)
{
	// open a blank window
	var aWindow = window.open('', 'TableAddRow2NewWindow',
	'scrollbars=yes,menubar=yes,resizable=yes,toolbar=no,width=400,height=400');
	
	// set the target to the blank window
	frm.target = 'TableAddRow2NewWindow';
	
	// submit
	frm.submit();
}

function delrow(moduleTable,l) {
len = l;
	for (i = len -1; i != 0; i--) {
	
		moduleTable.deleteRow(i);
	
	}
}
